﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.CRM;
using Jzy.Core.Common;
using Jzy.Core.Common.Web;

using Jzy.Model.CRM;
using Frame.Lib.Web;
using Frame.Lib.Utilities;

namespace Jzy.Web.CRM.Customer
{
    public partial class List : PageList
    {
        private CustomerBO _customer = new CustomerBO();
        private string strOrders;


        private CustomerBO Customer
        {
            get
            {
                if (_customer == null)
                {
                    _customer = new CustomerBO();
                }
                return _customer;
            }
        }


        private PagerSortSetting _setting;

        protected void Page_Load(object sender, EventArgs e)
        {
            _setting = this.Footer1.InitPager(new BindViewHandler(BindView));

            if (!IsPostBack)
            {

                Pager.BindDrp(drpPager);
                BindView();
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "客户");

            lnkAdd.HRef = "Edit.aspx";
            btnMove.Attributes.Add("onclick", "moveDept();");
            JSUtil.ClickBtn(lnkMove, btnMove);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);

        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);

            _setting.Input = txtSearch.Text.Trim();

            Pager.SetPageSize(_setting, drpPager);

            string condition = "IsDelete=0";

            if (txtSearch.Text.Trim() != "")
            {
                condition += " and Name like '%" + txtSearch.Text.Trim() + "%'";
            }


            IEnumerable<CustomerInfo> list = this.Customer.GetList(_setting, condition).OrderBy(Info => Info.Orders).ThenByDescending(Info => Info.InputTime);



            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + _setting.RecordCount + "</b>&nbsp;个客户&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();


        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            //要删除相关的图片和附件
            this.Customer.LogicDelete(strSelectIDs);
            BindView();
        }

        protected void btnSaveSequence_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            List<int> orders = new List<int>();
            StrUtil.SplitIDOrders(ids, orders, strOrders);
            this.Customer.SaveOrders(ids, orders);
            BindView();
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                lnkName.NavigateUrl = lnkEdit.NavigateUrl = "Edit.aspx?CustomerID=" + strID;

            }
        }
    }
}
